package com.itelite.controller;

import com.itelite.aop.WinControllerPointCut;
import com.itelite.common.info.RedisKey;
import com.itelite.common.info.Result;
import com.itelite.common.util.FileUtils;
import com.itelite.common.util.SecurityUtils;
import com.itelite.domain.dto.Carousel;
import com.itelite.domain.pojo.Group;
import com.itelite.service.ConfigService;
import com.itelite.service.GroupService;
import com.itelite.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author 沈洋 邮箱:1845973183@qq.com
 * @create 2021/8/9-15:01
 **/
@Slf4j
@Api("配置模块")
@RequestMapping("/config/api")
@RestController
public class ConfigController {
    @Autowired
    private ConfigService configService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private GroupService groupService;
    @ApiOperation("首页轮播图")
    @GetMapping("/carousel")
    public Result carousel(){
        List<Carousel> carousel = configService.getCarousel();
        return Result.success()
                .data("carousel",carousel);
    }
    @ApiOperation("首页背景图")
    @GetMapping("/background")
    public Result background(){
        return Result.success()
                .data("background",configService.getBackGround().getConfig());
    }

    @ApiOperation("删除轮播图")
    @GetMapping("/deleteCarousel/{carouselId}")
    @WinControllerPointCut
    public Result deleteCarousel(@PathVariable Integer carouselId){
        if (configService.deleteCarousel(carouselId)) {
            //清除缓存
            redisService.removeList(RedisKey.CAROUSEL);
            return Result.success();
        }
        return Result.failure();
    }

    @ApiOperation("上传轮播图")
    @PostMapping("/uploadCarousel")
    @WinControllerPointCut
    public Result uploadCarousel(MultipartFile file){
        if (!FileUtils.imageExtensionName.contains(file.getContentType()))
            return Result.fileError();
        Result result = null;
        if (configService.uploadCarousel(file)) {
            log.info("上传的轮播图："+file.getName());
            redisService.removeList(RedisKey.CAROUSEL);
            result = Result.success();
        }else {
            result = Result.failure();
        }
        return result;
    }
    @ApiOperation("上传背景图")
    @PostMapping("/uploadBackGroup")
    @WinControllerPointCut
    public Result uploadBackGroup(MultipartFile file){
        if (!FileUtils.imageExtensionName.contains(file.getContentType()))
            return Result.fileError();
        Result result = null;
        if (configService.uploadBackGroup(file)) {
            log.info("上传的背景图："+file.getName());
            result = Result.success();
        }else result = Result.failure();
        return result;
    }
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("设置全局发送邮件状态")
    @WinControllerPointCut
    @GetMapping("/changeSendState/{state}")
    public Result changeSendState(@PathVariable Integer state){
        if (state==1){
            log.info("全局发送邮件已开启");
        }else if (state==-1){
            log.info("全局发送邮件已关闭");
        }else return Result.paramError();
        configService.updateSendState(state);
        return Result.success();
    }
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("获取全局邮件发送配置")
    @GetMapping("/getSendState")
    public Result getSendState(){
        int state = configService.getSendState();
        return Result.success().data("state",state);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("获取邮件发送名单")
    @GetMapping("/sendGroup")
    public Result sendGroup(){
        List<Group> groups = groupService.getSendEmailGroup();
        return Result.success().data("groups",groups);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("修改某组发送邮件状态（state=0不会发送，state=1会发送）")
    @GetMapping("/changeGroupSend/{groupId}/{state}")
    public Result changeGroupSend(@PathVariable Integer groupId ,@PathVariable Integer state){
        if (state==1){
            log.info("小组"+groupId+"开启邮件发送");
        }else if (state==-1){
            log.info("小组"+groupId+"关闭邮件发送");
        }else return Result.paramError();
        groupService.updateGroupSendState(groupId,state);
        return Result.success();
    }

    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("导入名单时存在的小组")
    @GetMapping("/importableGroup")
    public Result importableGroup(){
        List<Group> groups = groupService.getImportableGroup1();
        return Result.success().data("groups",groups);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("导入名单时存在的小组（state=0不出现，state=1出现）")
    @GetMapping("/changeImportableGroup/{groupId}/{state}")
    @WinControllerPointCut
    public Result changeImportableGroup(@PathVariable Integer groupId,@PathVariable Integer state){
        if (state==1){
            log.info("小组"+groupId+"加入导入名单");
        }else if (state==-1){
            log.info("小组"+groupId+"移出导入名单");
        }else return Result.paramError();
        groupService.updateGroupImportable(groupId,state);
        return Result.success();
    }
}
